package com.bdth.service.impl;

import com.bdth.mapper.BaseMapper;
import com.bdth.service.IBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class BaseServiceImpl<T> implements IBaseService<T> {

	@Autowired
	public BaseMapper<T> mapper;

	@Autowired
	private RedisTemplate redisTemplate;

	Collection<String> colls = new ArrayList<String>();

	public void add(T t) throws Exception {
		mapper.insertSelective(t);
	}

	public void update(T t) throws Exception {
		mapper.updateByPrimaryKey(t);
	}

	public void updateBySelective(T t) {
		mapper.updateByPrimaryKeySelective(t);
	}

	public void delete(Object... ids) throws Exception {
		if (ids == null || ids.length < 1) {
			return;
		}
		for (Object id : ids) {
			mapper.deleteByPrimaryKey(id);
		}
	}

	public void deleteFake(T t) throws Exception {
		mapper.updateByPrimaryKeySelective(t);
	}


	public T queryById(Object id) throws Exception {
		return mapper.selectByPrimaryKey(id);
	}

	public int queryByMapCount(Map map) throws Exception {
		Integer rowCount = mapper.queryByMapCount(map);
		return rowCount;
	}

	public List<T> queryByMapList(Map map) throws Exception {
	/*	ValueOperations<String, String> stringOperations = redisTemplate
				.opsForValue();
		// String类型数据存储，不设置过期时间，永久性保存
		stringOperations.set("string1", "fiala");
		// String类型数据存储，设置过期时间为80秒，采用TimeUnit控制时间单位
		stringOperations.set("string2", "fiala", 80, TimeUnit.SECONDS);
		// 判断key值是否存在，存在则不存储，不存在则存储
		stringOperations.setIfAbsent("string1", "my fiala");
		stringOperations.setIfAbsent("string3", "my fiala");
		String value1 = stringOperations.get("string1");
		String value2 = stringOperations.get("string3");
		System.out.println(value1);
		System.out.println(value2);*/
		return mapper.queryByMapList(map);
	}
}
